//
//  YTCmdSocketManager.m
//  YUNT
//
//  Created by 梁炜东 on 2017/11/28.
//  Copyright © 2017年 梁炜东. All rights reserved.
//

#import "YTCmdSocketManager.h"
#import "YTBleManager.h"

/** 应答式序号 */
static int num = 1;

@interface YTCmdSocketManager ()<YTBleManagerDelegate>

@property(nonatomic, strong)NSMutableDictionary *msgDict;

@end

@implementation YTCmdSocketManager

/*
 *发送协议数据 使用应答序列号的
 */
- (void)sendRequestData:(NSMutableData *)data WithnSocketId:(NSString *)socketId Block:(sendBlock)block
{
    NSLog(@"上行数据1：%@", data);
    @synchronized(_msgDict)
    {
        if (block && socketId)
        {
            [_msgDict setObject:[block copy] forKey:socketId];
        }
    }
    [[YTBleManager shareModel] sendBleData:data Block:block];
}

-(void)delegateBleData:(NSMutableArray *)data
{
    NSString *socketId = [NSString stringWithFormat:@"%@%@", data[2], data[3]];
    sendBlock block = [_msgDict objectForKey:socketId];
    if (block)
    {
        block(data);
    }
}

+(instancetype)shareToolManager
{
    static YTCmdSocketManager *shareManager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shareManager = [[YTCmdSocketManager alloc] init];
    });
    return shareManager;
}

- (instancetype)init
{
    self = [super init];
    if (self)
    {
        _msgDict = [NSMutableDictionary dictionary];
        [YTBleManager shareModel].delegate = self;
    }
    return self;
}
/** 获得应答式序号 */
- (int)addValue
{
    num = num + 1;
    if (num > 65535)
    {
        num = 1;
    }
    return num;
}
/** 获取漂移微调 */
- (void)getDriftBlock:(sendBlock)block
{
    uint8_t b = 0;
    uint8_t sum = 0;
    NSMutableData *ret = [[NSMutableData alloc] init];
    int order = [self addValue];
    NSMutableArray *arrSerial = [NSMutableArray array];
    
    b = 0xA0; [ret appendBytes:&b length:1]; sum += b;
    b = 0x07; [ret appendBytes:&b length:1]; sum += b;
    b = 0xBC; [ret appendBytes:&b length:1]; sum += b;
    b = 0xE6; [ret appendBytes:&b length:1]; sum += b;
    b = (order & 0XFF); [ret appendBytes:&b length:1];
    [arrSerial addObject:[NSNumber numberWithUnsignedChar:b]];
    sum += b;
    b = (order >> 8);   [ret appendBytes:&b length:1];
    [arrSerial addObject:[NSNumber numberWithUnsignedChar:b]];
    sum += b;
    b = sum & 255;      [ret appendBytes:&b length:1];
    [self sendRequestData:ret WithnSocketId:@"bce6" Block:block];
}

/** 设置漂移微调 */
- (void)setDriftValue:(NSArray *)driftArr Block:(sendBlock)block
{
    uint8_t b = 0;
    uint8_t sum = 0;
    NSMutableData *ret = [[NSMutableData alloc] init];
    int order = [self addValue];
    NSMutableArray *arrSerial = [NSMutableArray array];
    
    b = 0xA0; [ret appendBytes:&b length:1]; sum += b;
    b = 0x0a; [ret appendBytes:&b length:1]; sum += b;
    b = 0xBC; [ret appendBytes:&b length:1]; sum += b;
    b = 0x66; [ret appendBytes:&b length:1]; sum += b;
    b=(order & 0xFF); [ret appendBytes:&b length:1];
    [arrSerial addObject:[NSNumber numberWithUnsignedChar:b]];
    sum += b;
    b=(order>>8);     [ret appendBytes:&b length:1];
    [arrSerial addObject:[NSNumber numberWithUnsignedChar:b]];
    sum += b;
    //航向轴
    int temp1 = [driftArr[0] intValue];
    b = temp1; [ret appendBytes:&b length:1]; sum += b;
    //横滚轴
    int temp2 = [driftArr[1] intValue];
    b = temp2; [ret appendBytes:&b length:1]; sum += b;
    //俯仰轴
    int temp3 = [driftArr[2] intValue];
    b = temp3; [ret appendBytes:&b length:1]; sum += b;
    
    b = sum & 255; [ret appendBytes:&b length:1];
    [self sendRequestData:ret WithnSocketId:@"bc66" Block:block];
}

/** 设置回中模式 */
- (void)setHuiZhongBlock:(sendBlock)block
{
    uint8_t b = 0;
    uint8_t sum = 0;
    NSMutableData *ret = [[NSMutableData alloc] init];
    int order = [self addValue];
    NSMutableArray *arrSerial = [NSMutableArray array];
    
    b = 0xA0; [ret appendBytes:&b length:1]; sum += b;
    b = 0x07; [ret appendBytes:&b length:1]; sum += b;
    b = 0xBC; [ret appendBytes:&b length:1]; sum += b;
    b = 0x65; [ret appendBytes:&b length:1]; sum += b;
    b = (order & 0XFF); [ret appendBytes:&b length:1];
    [arrSerial addObject:[NSNumber numberWithUnsignedChar:b]];
    sum += b;
    b = (order >> 8);   [ret appendBytes:&b length:1];
    [arrSerial addObject:[NSNumber numberWithUnsignedChar:b]];
    sum += b;
    b = sum & 255;      [ret appendBytes:&b length:1];
    [self sendRequestData:ret WithnSocketId:@"bc65" Block:block];
}

/** 获取跟随速度,死区,模式等参数 */
- (void)getCloudALLParam:(int)model Block:(sendBlock)block
{
    NSString *socketId = nil;
    if (model == 1333)
    {
        socketId = @"bce1N";
    }else if (model == 1500)
    {
        socketId = @"bce1S";
    }
    uint8_t b = 0;
    uint8_t sum = 0;
    NSMutableData *ret = [[NSMutableData alloc] init];
    int order = [self addValue];
    NSMutableArray *arrSerial = [NSMutableArray array];
    
    b = 0xA0; [ret appendBytes:&b length:1]; sum += b;
    b = 0x09; [ret appendBytes:&b length:1]; sum += b;
    b = 0xBC; [ret appendBytes:&b length:1]; sum += b;
    b = 0xE1; [ret appendBytes:&b length:1]; sum += b;
    b = (order & 0XFF); [ret appendBytes:&b length:1];
    [arrSerial addObject:[NSNumber numberWithUnsignedChar:b]];
    sum += b;
    b = (order >> 8);   [ret appendBytes:&b length:1];
    [arrSerial addObject:[NSNumber numberWithUnsignedChar:b]];
    sum += b;
    
    Byte *buff = malloc(1024*2);
    buff =(Byte *)&model;
    b = buff[1]; [ret appendBytes:&b length:1]; sum += b;
    b = buff[0]; [ret appendBytes:&b length:1]; sum += b;
    
    b = sum & 255;      [ret appendBytes:&b length:1];
    [self sendRequestData:ret WithnSocketId:socketId Block:block];
    //内存释放
//        free(buff);
}

/** 设置跟随速度,死区,模式等参数 */
- (void)setCloudAllParam:(int)model Sudusiqu:(NSArray *)sudusiqu Block:(sendBlock)block
{
    uint8_t b = 0;
    uint8_t sum = 0;
    NSMutableData *ret = [[NSMutableData alloc] init];
    int order = [self addValue];
    NSMutableArray *arrSerial = [NSMutableArray array];
    
    b = 0xA0; [ret appendBytes:&b length:1]; sum += b;
    b = 0x0D; [ret appendBytes:&b length:1]; sum += b;
    b = 0xBC; [ret appendBytes:&b length:1]; sum += b;
    b = 0x61; [ret appendBytes:&b length:1]; sum += b;
    b = (order & 0XFF); [ret appendBytes:&b length:1];
    [arrSerial addObject:[NSNumber numberWithUnsignedChar:b]];
    sum += b;
    b = (order >> 8);   [ret appendBytes:&b length:1];
    [arrSerial addObject:[NSNumber numberWithUnsignedChar:b]];
    sum += b;
    
    Byte *buff1 = malloc(1024*2);
    buff1 =(Byte *)&model;
    b = buff1[1]; [ret appendBytes:&b length:1]; sum += b;
    b = buff1[0]; [ret appendBytes:&b length:1]; sum += b;
    
    
    if (sudusiqu)
    {
        //航向跟随速度
        int temp1 = [sudusiqu[0][0] intValue];
        b = temp1; [ret appendBytes:&b length:1]; sum += b;
        //航向死区
        int temp2 = [sudusiqu[0][1] intValue];
        b = temp2; [ret appendBytes:&b length:1]; sum += b;
        //俯仰跟随速度
        int temp3 = [sudusiqu[1][0] intValue];
        b = temp3; [ret appendBytes:&b length:1]; sum += b;
        //俯仰死区
        int temp4 = [sudusiqu[1][1] intValue];
        b = temp4; [ret appendBytes:&b length:1]; sum += b;
    }
    b = sum & 255;      [ret appendBytes:&b length:1];
    [self sendRequestData:ret WithnSocketId:@"bc61" Block:block];
    //内存释放
    //    free(buff1);
}
/** 重置云台参数 */
- (void)resetCloudParamsBlock:(sendBlock)block
{
    uint8_t b = 0;
    uint8_t sum = 0;
    NSMutableData *ret = [[NSMutableData alloc] init];
    int order = [self addValue];
    NSMutableArray *arrSerial = [NSMutableArray array];
    
    b = 0xA0; [ret appendBytes:&b length:1]; sum += b;
    b = 0x08; [ret appendBytes:&b length:1]; sum += b;
    b = 0xBC; [ret appendBytes:&b length:1]; sum += b;
    b = 0x64; [ret appendBytes:&b length:1]; sum += b;
    b = (order & 0XFF); [ret appendBytes:&b length:1];
    [arrSerial addObject:[NSNumber numberWithUnsignedChar:b]];
    sum += b;
    b = (order >> 8);   [ret appendBytes:&b length:1];
    [arrSerial addObject:[NSNumber numberWithUnsignedChar:b]];
    sum += b;
    b = 0x01; [ret appendBytes:&b length:1]; sum += b;
    b = sum & 255;      [ret appendBytes:&b length:1];
    [self sendRequestData:ret WithnSocketId:@"bc64" Block:block];
}

/** 初始化云台校准 */
- (void)resetCloudCalibrateBlock:(sendBlock)block
{
    uint8_t b = 0;
    uint8_t sum = 0;
    NSMutableData *ret = [[NSMutableData alloc] init];
    int order = [self addValue];
    NSMutableArray *arrSerial = [NSMutableArray array];
    
    b = 0xA0; [ret appendBytes:&b length:1]; sum += b;
    b = 0x08; [ret appendBytes:&b length:1]; sum += b;
    b = 0xBC; [ret appendBytes:&b length:1]; sum += b;
    b = 0x63; [ret appendBytes:&b length:1]; sum += b;
    b = (order & 0XFF); [ret appendBytes:&b length:1];
    [arrSerial addObject:[NSNumber numberWithUnsignedChar:b]];
    sum += b;
    b = (order >> 8);   [ret appendBytes:&b length:1];
    [arrSerial addObject:[NSNumber numberWithUnsignedChar:b]];
    sum += b;
    b = 0x01; [ret appendBytes:&b length:1]; sum += b;
    b = sum & 255;      [ret appendBytes:&b length:1];
    [self sendRequestData:ret WithnSocketId:@"bc63" Block:block];
}

/** 获取云台版本号 */
- (void)getCloudVersionBlock:(sendBlock)block
{
    uint8_t b = 0;
    uint8_t sum = 0;
    NSMutableData *ret = [[NSMutableData alloc] init];
    int order = [self addValue];
    NSMutableArray *arrSerial = [NSMutableArray array];
    
    b = 0xA0; [ret appendBytes:&b length:1]; sum += b;
    b = 0x07; [ret appendBytes:&b length:1]; sum += b;
    b = 0xBC; [ret appendBytes:&b length:1]; sum += b;
    b = 0xB3; [ret appendBytes:&b length:1]; sum += b;
    b = (order & 0XFF); [ret appendBytes:&b length:1];
    [arrSerial addObject:[NSNumber numberWithUnsignedChar:b]];
    sum += b;
    b = (order >> 8);   [ret appendBytes:&b length:1];
    [arrSerial addObject:[NSNumber numberWithUnsignedChar:b]];
    sum += b;
    b = sum & 255;      [ret appendBytes:&b length:1];
    [self sendRequestData:ret WithnSocketId:@"bcb3" Block:block];
}
- (void)sendPitchAndCourseWithPitch:(int)pitch WithCourse:(int)course Block:(sendBlock)block
{
    uint8_t b = 0;
    uint8_t sum = 0;
    NSMutableData *ret = [[NSMutableData alloc] init];
    int order = [self addValue];
    NSMutableArray *arrSerial = [NSMutableArray array];
    
    b = 0xA0; [ret appendBytes:&b length:1]; sum += b;
    b = 0x0B; [ret appendBytes:&b length:1]; sum += b;
    b = 0xBC; [ret appendBytes:&b length:1]; sum += b;
    b = 0x60; [ret appendBytes:&b length:1]; sum += b;
    b = (order & 0XFF); [ret appendBytes:&b length:1];
    [arrSerial addObject:[NSNumber numberWithUnsignedChar:b]];
    sum += b;
    b = (order >> 8);   [ret appendBytes:&b length:1];
    [arrSerial addObject:[NSNumber numberWithUnsignedChar:b]];
    sum += b;
    
    Byte *buff1 = malloc(1024*2);
    buff1 =(Byte *)&pitch;
    b = buff1[1]; [ret appendBytes:&b length:1]; sum += b;
    b = buff1[0]; [ret appendBytes:&b length:1]; sum += b;
    
    Byte *buff2 = malloc(1024*2);
    buff2 =(Byte *)&course;
    b = buff2[1]; [ret appendBytes:&b length:1]; sum += b;
    b = buff2[0]; [ret appendBytes:&b length:1]; sum += b;
    
    b = sum & 255;      [ret appendBytes:&b length:1];
    [self sendRequestData:ret WithnSocketId:@"bc60" Block:block];
    //内存释放
    //    free(buff1);
    //    free(buff2);
}
- (void)setModeParamWithMoshi:(int)moshi Block:(sendBlock)block
{
    uint8_t b = 0;
    uint8_t sum = 0;
    NSMutableData *ret = [[NSMutableData alloc] init];
    int order = [self addValue];
    NSMutableArray *arrSerial = [NSMutableArray array];
    
    b = 0xA0; [ret appendBytes:&b length:1]; sum += b;
    b = 0x09; [ret appendBytes:&b length:1]; sum += b;
    b = 0xBC; [ret appendBytes:&b length:1]; sum += b;
    b = 0x62; [ret appendBytes:&b length:1]; sum += b;
    b = (order & 0XFF); [ret appendBytes:&b length:1];
    [arrSerial addObject:[NSNumber numberWithUnsignedChar:b]];
    sum += b;
    b = (order >> 8);   [ret appendBytes:&b length:1];
    [arrSerial addObject:[NSNumber numberWithUnsignedChar:b]];
    sum += b;
    
    Byte *buff1 = malloc(1024*2);
    buff1 =(Byte *)&moshi;
    b = buff1[1]; [ret appendBytes:&b length:1]; sum += b;
    b = buff1[0]; [ret appendBytes:&b length:1]; sum += b;
    
    b = sum & 255;      [ret appendBytes:&b length:1];
    [self sendRequestData:ret WithnSocketId:@"bc62" Block:block];
    //内存释放
    //    free(buff1);
}

/** 读取云台当前模式 */
- (void)getCloudModeBlock:(sendBlock)block
{
    uint8_t b = 0;
    uint8_t sum = 0;
    NSMutableData *ret = [[NSMutableData alloc] init];
    int order = [self addValue];
    NSMutableArray *arrSerial = [NSMutableArray array];
    
    b = 0xA0; [ret appendBytes:&b length:1]; sum += b;
    b = 0x09; [ret appendBytes:&b length:1]; sum += b;
    b = 0xBC; [ret appendBytes:&b length:1]; sum += b;
    b = 0xE2; [ret appendBytes:&b length:1]; sum += b;
    b = (order & 0XFF); [ret appendBytes:&b length:1];
    [arrSerial addObject:[NSNumber numberWithUnsignedChar:b]];
    sum += b;
    b = (order >> 8);   [ret appendBytes:&b length:1];
    [arrSerial addObject:[NSNumber numberWithUnsignedChar:b]];
    sum += b;
    b = 0x00; [ret appendBytes:&b length:1]; sum += b;
    b = 0x00; [ret appendBytes:&b length:1]; sum += b;
    b = sum & 255;      [ret appendBytes:&b length:1];
    [self sendRequestData:ret WithnSocketId:@"bce2" Block:block];
}

-(void)registerAutoDataSocketId:(NSString *)socketId WithSuccess:(sendBlock)block
{
//    [socketManager sendAutoSocketId:socketId WithSuccess:^(NSMutableArray *data)
//     {
//         block(data);
//     }];
}

@end
